package module::carters_us::default;
use Encode;
use vars qw($VERSION @ISA @EXPORT);
$VERSION = 1.00;
@ISA     = qw(Module::Module);
@EXPORT  = ();

# ----------------------------------------------------
# constructor
# Modify Log
# ----------------------------------------------------
sub new {
	my $class = shift;
#	my %parm  = @_;
	my $this  = {};
	bless $this, $class;
	return $this;
}

# ----------------------------------------------------
# get all product's url and categories then write them
# into temp xml
# Modify Louise 14/10/2013 #.... ####
# ----------------------------------------------------

sub filter {
    
	#A200000 Filter retailer website starts
    Log::Write::setLog('A200000');
    my $this = shift;
    my $writexml = new Feed::XML();
    my $data = $main::filedata;
    my $hostURL = $this -> { 'config' } -> { 'host_url' };
    my $catalog = { 'Product' => [] };
    my $charSet = 'utf-8';
    my $splitString = '<split>';
    my $parserObj   = new module::carters_us::parser;
    
    # get categoryUrls
    my @categoryUrlArray;
    @categoryUrlArray = $this -> getCategoryUrls ( $hostURL, $charSet );
    
    if ( !@categoryUrlArray ) {
        # E200000: filter failed
        Log::Write::setLog ( 'E200000' );
        return;
    }
    
   # get all page urls all product links
    my @allProducts = $this -> getFilterProduct( $charSet, \@categoryUrlArray );

    # write tree date into XML file
    if ( @allProducts ) {
        $catalog -> { 'Product' } = \@allProducts;
        $writexml->addXML ( $catalog, $this -> { 'config' } -> { 'data_filter_file' } );

        # A200002the total count of detail page urls
        Log::Write::setLog ( 'A200002', { 'COUNT' => scalar( @allProducts ) } );
    } else {

        # E200000filter failed
        Log::Write::setLog ( 'E200000' );
    }

    # A299999Filter retailer website end.
    Log::Write::setLog ( 'A299999' );
    
}

# ----------------------------------------------------
# parse all products detail page and write the infos
# into final XML feed
# Modify Louise 14/10/2013 #.... ####
# ----------------------------------------------------
sub parser {
	
	# A300000Parser detail page starts.
	Log::Write::setLog ( 'A300000' );
	my $this = shift;
	my $brand = 'Carter\'s';
	
	# new parser object
	my $parserObj = module::carters_us::parser -> new;

	# new $writexml object, used for read and write XML info
	my $writeXml = new Feed::XML();
	
	#read xml create by filter to get all products infos(url and category)
	my @xmlInfoArray = $writeXml -> readXML ( $this -> { 'config' } -> { 'data_filter_file' } );

	my $count = 0;
	my $total = 0;
	my $catalog = { 'Product' => [] };
	
	for my $oneTemp ( @xmlInfoArray ) {
        my %oneTempInfoHash = %{$oneTemp};
        my $url = ( @{ $oneTempInfoHash{"ProductURL"} } )[0];
        my $pid = ( @{ $oneTempInfoHash{"ProductId"} } )[0];
        my %productInfoHash;
        eval {
            # get infos from product detail page and save it in hash variable
            %productInfoHash = $parserObj -> parseAndgetInfo ( $url, $pid );
        };
        if ($@) {
            # E300003 : Failed to extract information from XXX page.
            Log::Write::setLog ( "E300003", { 'URL' => $url } );
            next;
        }
        if ( %productInfoHash ) {
            my $product = {
                'Retailer'    => $this -> {'config'} -> {'retailer_name'},
                'ProductId'   => $productInfoHash{'ProductId'},
                'Name'        => $productInfoHash{'Name'},
                'Brand'       => $brand,
                'Price'       => $productInfoHash{'Price'},
                'SalePrice'   => $productInfoHash{'SalePrice'},
                'Description' => $productInfoHash{'Description'},
                'ProductURL'  => $oneTempInfoHash{'ProductURL'},
                'ImageURL'    => $productInfoHash{'ImageURL'},
                'Category'    => $oneTempInfoHash{'Category'},
                'Size'        => $productInfoHash{'Size'},
                'Color'       => $productInfoHash{'Color'},
                'Time'        => $productInfoHash{'Time'}
            };

            $catalog -> { 'Product' } = [ @{ $catalog -> { 'Product' } }, $product ];

            $count++;
            $total++;

            # write data into XML per 100(configurable) records
            if ( $count == $this -> { 'config' } -> { 'write_frequency' } ) {
                $writeXml -> addXML ( $catalog, $this -> { 'config' } -> { 'data_feed_file' } );
                $catalog = { 'Product' => [] };

                # A300001 : Record XXX prodcuts have been appended to feed file.
                Log::Write::setLog ( "A300001", { 'COUNT' => $count } );
                $count = 0;
            }
        }
    }
    $writeXml -> addXML ( $catalog, $this -> { 'config' } -> { 'data_feed_file' } );
    # A300001 : Record XXX prodcuts have been appended to feed file.
    Log::Write::setLog ( "A300001", { 'COUNT' => $count } );

    if ( $total ) {
       # A300003 : Record XXX prodcuts have been written into feed file totally.
        Log::Write::setLog ( 'A300003', { 'COUNT' => $total } );

        # A399999 : Parser detail page end.
        Log::Write::setLog ( 'A399999' );
    } else {
        # E300000 : Parser failed.
        Log::Write::setLog ( 'E300000' );
    }
}

# ----------------------------------------------------
# get all category urls
# Modify Log
# ----------------------------------------------------

sub getCategoryUrls {

    my $this = shift;
    my $url = shift;
    my $charSet = shift;
    my $data = $main::filedata;
    my @categoryUrlArray;
    my $splitString = '<split>';
    my $parserObj   = new module::carters_us::parser;
    my $content = $data->getPage( $url, $charSet );
    
    # get page failed
    if ( !$content ) {
        # E200001 Failed to get XX entrance information.
        Log::Write::setLog ( 'E200001', { 'URL' => $url } );
        next;
    }
    my $cat1name;
    my $cat2name;
    my $cat2Url;
    my @cat2Array;
    my @cat3Array;
    my @cat4Array;
    while ($content =~ /<a[^>]*name=["']Header:[^"']*["'][^>]*?class="pngFix">\s*(.*?)\s*<\/a>(.*?)<hr class="end">/isg){
        $cat1name = $1;
        my $catTxt = $2;
        while($catTxt =~ /<h3[^>]*>.*?href="([^"]+)"[^>]*>(.*?)<\/a>/isg){
            $cat2Url = $1;
            $cat2name = $2;
            $cat2name =~ s/<.*?>//isg;
            push @cat2Array, $cat2Url . $splitString .$cat1name. $splitString .$cat2name;
        }
    }
#   get leve3 & leve4 category
    foreach my $item (@cat2Array){
        my ( $url, @catNameArray ) = split ($splitString, $item );
        my $cat2Str = join($splitString, @catNameArray);
        my $content = $data->getPage( $url, $charSet );
        my $cat2Txt;
        my $cat3Name;
        my $cat4Name;
        my $cat3Url;
        my $cat4Url;
        my $cat3Txt;
        
        if ( !$content ) {
        # E300001 : Failed to get detail information of XXX.
        Log::Write::setLog( "E300001", { 'URL' => $url } );
        return;
    	}
    	else {
        	$content =~ s/<!--.*?-->//isg;
    	}
        
        if($content =~ /<h3[^>]*>Categories(.*?)class="lnavrule"/is){
         	$cat2Txt = $1;
        }
        
    	while($cat2Txt =~ /class="cat_name" href="([^"]+)"[^>]*>([^<]+)<\/a>.*?class="subnav"[^>]*>(.*?)<\/ul>/isg){
        	$cat3Url = $1;
        	$cat3Name = $2;
        	$cat3Txt = $3;
        	$cat3Name =~ s/&amp;/&/isg;
        	$cat3Name =~ s/&#40;/\(/isg;
            $cat3Name =~ s/&#41;/\(/isg;
        	if($cat3Name =~ /Gift Cards/is){
            	next;
        	}
        	push @cat3Array, $cat3Url . $splitString .$cat2Str. $splitString .$cat3Name;
        	while($cat3Txt =~ /href="([^"]+)"[^>]*>([^<]+)<\/a>/isg){
            	$cat4Url = $1;
            	$cat4Name = $2;
            	$cat4Name =~ s/&amp;/&/isg;
            	$cat4Name =~ s/&#40;/\(/isg;
            	$cat4Name =~ s/&#41;/\(/isg;
            	push @cat4Array, $cat4Url . $splitString .$cat2Str. $splitString .$cat3Name. $splitString .$cat4Name;
        	}
    	}
    	
    	while ($cat3Txt =~ /<li\s*class="noSubCat"[^>]*>(.*?)<\/li>/isg){
        	my  $tempTxt = $1;
        	if($tempTxt =~ /href="([^"]+)"[^>]*>([^<]+)<\/a>/is){
            	my $cat3Url = $1;
            	my $cat3Name = $2;
            	$cat3Name =~ s/&amp;/&/isg;
            	$cat3Name =~ s/&#40;/\(/isg;
            	$cat3Name =~ s/&#41;/\(/isg;
            	if($cat3Name =~ /Gift Cards/is){
                	next;
            	}
            	push @cat3Array, $cat3Url . $splitString .$cat2Str. $splitString .$cat3Name;
        	}
    	}
	}
    @categoryUrlArray = (@cat3Array, @cat4Array);
    return @categoryUrlArray;
}

# ----------------------------------------------------
# get all product in filter xml file
# Modify Log
# ----------------------------------------------------
sub getFilterProduct {
    my $this = shift;
    my $charSet = shift;
    my $pageUrls = shift;
    my $data = $main::filedata;
    my @pageUrlList = @{$pageUrls};
    my $hostUrl = $this -> { 'config' } -> { 'host_url' };
    my @allProducts;
    my $productCount = 0;
#   my $signs= decode('utf8','$');
    
    for my $tempUrl ( @pageUrlList ) {
        $productCount = 0;
        my ( $url, @categoryArray ) = split ( '<split>', $tempUrl );
        $url .='&viewall=true'; 
        my $content = $data -> getPage ( $url, $charSet );
        if ( !$content ) {
            # E200005 Failed to get the main url of the website .
            Log::Write::setLog ( 'E200005', { 'URL' => $url } );
            next;
        }
#        if ( $content =~ /<div class="pagination">\s*<a href="(.*?)"/is ){
#        	$url = $1;
#        	$content = $data -> getPage ( $url, $charSet );
#	        if ( !$content ) {
#	            # E200005 Failed to get the main url of the website .
#	            Log::Write::setLog ( 'E200005', { 'URL' => $url } );
#	            next;
#	        }
#        }
#        if ( $content =~ m/class="products"(.*?)id="pageBottom"/is ) {
#            $content = $1;
#        }
        while($content =~ /<div class="product producttile"(.*?)class="promo"/isg){
            my $oneProductInfo = $1;
            if( $oneProductInfo =~/href="([^"]+)"/is){
                my $productUrl = $1;
                my $productId;
                if($productUrl =~ /([^\/,]+),default/is){
                    $productId = $1;
                }
                if ( !$productId ) {
                        # E200003 Failed to get product NAME from product URL.
                        Log::Write::setLog( 'E200003' , { 'URL' => $productUrl, 'POINT' => 'Product Id',} );
                        next;
                }
                my $product = {
                            'ProductId' => $productId,
                            'ProductURL'=> $productUrl,
                            'Category'  => [ { 'Part' => \@categoryArray } ]
                        };
                @allProducts = ( @allProducts, $product );
                $productCount++;
            }
        }
        Log::Write::setLog( 'A200001', { 'COUNT' => $productCount, 'URL' => $url } );
    }
    # A200002 The total count of the entryUrl obtained
#    Log::Write::setLog ( 'A200002', { 'COUNT' => $productCount } );
    return @allProducts;
}

1;
